package labrom.usage.server;


/**
 * The path info after the /test/v1/ segment.
 * 
 * Expected path info is app{app version}/{something}
 * 
 * @author Romain Laboisse
 *
 */
public class ApiV1TestPath {
	
	
	private static String APP_VERSION_PREFIX = "app";

	
	
	int appVersion;
	String what;
	
	
	ApiV1TestPath(String pathInfo) {
		if(pathInfo == null)
			throw new IllegalArgumentException("pathInfo is null");
		if(pathInfo.startsWith("/"))
			pathInfo = pathInfo.substring(1);
		String[] tokens = pathInfo.split("/");
		if(tokens.length != 2)
			throw new IllegalArgumentException("pathInfo has wrong format, should be {app version}/{client GUID}/{resource name}, got: " + pathInfo);
		
		try {
			if(!tokens[0].startsWith(APP_VERSION_PREFIX))
				throw new IllegalArgumentException("app version must start with '" + APP_VERSION_PREFIX + "'");
			appVersion = Integer.parseInt(tokens[0].substring(APP_VERSION_PREFIX.length()));
		} catch(Exception e) {
			throw new IllegalArgumentException(tokens[0] + " is not a valid app version number");
		}

		if(tokens[1].isEmpty())
			throw new IllegalArgumentException(tokens[1] + " is not a valid path segment, must not be empty");
		what = tokens[1];
	}
	
	

}
